package com.buss.admin.controller;

import java.util.Date;

import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.Update;
import org.jweb.core.bean.PageMode;
import org.jweb.core.query.hibernate.qbc.CriteriaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.buss.admin.service.IContentService;
import com.buss.admin.trans.form.ContentForm;
import com.buss.common.entity.BannerEntity;
import com.buss.common.model.json.EasyUIDataGridResult;
import com.buss.common.model.json.HXResult;
import com.buss.common.util.IDUtils;

@Controller
@RequestMapping("/admin/content")
public class ContentController {

	@Autowired
	private IContentService contentService;

	@RequestMapping("/contentlist")
	@ResponseBody
	public EasyUIDataGridResult getContentList(ContentForm contentForm, Long categoryId) {

		// 分页查询
		CriteriaQuery cq = contentForm.getCriteriaQuery();

		/*
		 * 带查询条件用过分类来查找
		 */
		if (categoryId != null) {
			cq.add(Restrictions.eq("cid", categoryId));
		}

		int pageNo = Integer.parseInt(contentForm.getPage());
		int pageSize = Integer.parseInt(contentForm.getRows());
		// 定义返回数据格式
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		PageMode<BannerEntity> pageMode = contentService.list4page(cq, pageNo, pageSize);

		// 封装数据
		result.setRows(pageMode.getResult());
		result.setTotal(pageMode.getTotal());
		return result;

	}

	@RequestMapping("/contentsave")
	@ResponseBody
	public HXResult saveOrUpdate(BannerEntity bannerEntity) {

		try {
			Long id = bannerEntity.getId();
			// 新增一条数据
			if (id == null) {
				long id2 = IDUtils.getArticleId();
				bannerEntity.setId(id2);
				Date date = new Date();
				bannerEntity.setUpdated(date);
				bannerEntity.setCreated(date);

				// 设置状态
				bannerEntity.setStatus(1);

				// 执行插入或更新数据操作
				contentService.saveOrUpdate(bannerEntity);
			} else {
				BannerEntity entity = contentService.get(BannerEntity.class, bannerEntity.getId());

				// 更新数据
				entity.setCid(bannerEntity.getCid());
				entity.setPic(bannerEntity.getPic());
				entity.setSortid(bannerEntity.getSortid());
				entity.setStatus(1);
				entity.setSubTitle(bannerEntity.getSubTitle());
				entity.setTitle(bannerEntity.getTitle());
				entity.setTitleDesc(bannerEntity.getTitleDesc());
				entity.setUpdated(new Date());
				entity.setUrl(bannerEntity.getUrl());

				contentService.saveOrUpdate(entity);

			}

			return HXResult.ok("操作数据成功！");

		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "服务器繁忙，操作失败！");
		}

	}

	@RequestMapping("/delete")
	@ResponseBody
	public HXResult delete(String ids) {
		String[] idStrings = ids.split(",");

		try {
			for (String id : idStrings) {

				BannerEntity bannerEntity = contentService.get(BannerEntity.class, Long.parseLong(id));

				contentService.delete(bannerEntity);
			}
			return HXResult.ok("删除数据成功！");

		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "服务器繁忙，删除数据失败！");
		}

	}

	/*
	 * 角色相关页面跳转
	 */
	@RequestMapping("/{path}")
	public String goPath(@PathVariable String path) {
		System.out.println("path:" + path);
		return "admin/content/" + path;
	}

}
